home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / linux / tools / amiga / gzip-1.1.2.lha / gzip-1.1.2 / unlzw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-03  |  8.5 KB  |  376 lines

  1. /* unlzw.c -- decompress files in LZW format.
  2.  * The code in this file is directly derived from the public domain 'compress'
  3.  * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
  4.  * Ken Turkowski, Dave Mack and Peter Jannesen.
  5.  *
  6.  * This is a temporary version which will be rewritten in some future version
  7.  * to accommodate in-memory decompression.
  8.  */
  9.  
  10. #ifndef lint
  11. static char rcsid[] = "$Id: unlzw.c,v 0.13 1993/05/27 10:32:55 jloup Exp $";
  12. #endif
  13.  
  14. #include <stdio.h>
  15. #include <sys/types.h>
  16.  
  17. #include "tailor.h"
  18.  
  19. #ifdef HAVE_UNISTD_H
  20. #  include <unistd.h>
  21. #endif
  22. #ifndef NO_FCNTL_H
  23. #  include <fcntl.h>
  24. #endif
  25.  
  26. #include "gzip.h"
  27. #include "lzw.h"
  28.  
  29. typedef    unsigned char    char_type;
  30. typedef          long   code_int;
  31. typedef unsigned long     count_int;
  32. typedef unsigned short    count_short;
  33. typedef unsigned long     cmp_code_int;
  34.  
  35. #define MAXCODE(n)    (1L << (n))
  36.     
  37. #ifndef    REGISTERS
  38. #    define    REGISTERS    2
  39. #endif
  40. #define    REG1    
  41. #define    REG2    
  42. #define    REG3    
  43. #define    REG4    
  44. #define    REG5    
  45. #define    REG6    
  46. #define    REG7    
  47. #define    REG8    
  48. #define    REG9    
  49. #define    REG10
  50. #define    REG11    
  51. #define    REG12    
  52. #define    REG13
  53. #define    REG14
  54. #define    REG15
  55. #define    REG16
  56. #if REGISTERS >= 1
  57. #    undef    REG1
  58. #    define    REG1    register
  59. #endif
  60. #if REGISTERS >= 2
  61. #    undef    REG2
  62. #    define    REG2    register
  63. #endif
  64. #if REGISTERS >= 3
  65. #    undef    REG3
  66. #    define    REG3    register
  67. #endif
  68. #if REGISTERS >= 4
  69. #    undef    REG4
  70. #    define    REG4    register
  71. #endif
  72. #if REGISTERS >= 5
  73. #    undef    REG5
  74. #    define    REG5    register
  75. #endif
  76. #if REGISTERS >= 6
  77. #    undef    REG6
  78. #    define    REG6    register
  79. #endif
  80. #if REGISTERS >= 7
  81. #    undef    REG7
  82. #    define    REG7    register
  83. #endif
  84. #if REGISTERS >= 8
  85. #    undef    REG8
  86. #    define    REG8    register
  87. #endif
  88. #if REGISTERS >= 9
  89. #    undef    REG9
  90. #    define    REG9    register
  91. #endif
  92. #if REGISTERS >= 10
  93. #    undef    REG10
  94. #    define    REG10    register
  95. #endif
  96. #if REGISTERS >= 11
  97. #    undef    REG11
  98. #    define    REG11    register
  99. #endif
  100. #if REGISTERS >= 12
  101. #    undef    REG12
  102. #    define    REG12    register
  103. #endif
  104. #if REGISTERS >= 13
  105. #    undef    REG13
  106. #    define    REG13    register
  107. #endif
  108. #if REGISTERS >= 14
  109. #    undef    REG14
  110. #    define    REG14    register
  111. #endif
  112. #if REGISTERS >= 15
  113. #    undef    REG15
  114. #    define    REG15    register
  115. #endif
  116. #if REGISTERS >= 16
  117. #    undef    REG16
  118. #    define    REG16    register
  119. #endif
  120.     
  121. #ifndef    BYTEORDER
  122. #    define    BYTEORDER    0000
  123. #endif
  124.     
  125. #ifndef    NOALLIGN
  126. #    define    NOALLIGN    0
  127. #endif
  128.  
  129.  
  130. union    bytes {
  131.     long  word;
  132.     struct {
  133. #if BYTEORDER == 4321
  134.     char_type    b1;
  135.     char_type    b2;
  136.     char_type    b3;
  137.     char_type    b4;
  138. #else
  139. #if BYTEORDER == 1234
  140.     char_type    b4;
  141.     char_type    b3;
  142.     char_type    b2;
  143.     char_type    b1;
  144. #else
  145. #    undef    BYTEORDER
  146.     int  dummy;
  147. #endif
  148. #endif
  149.     } bytes;
  150. };
  151.  
  152. #if BYTEORDER == 4321 && NOALLIGN == 1
  153. #  define input(b,o,c,n,m){ \
  154.      (c) = (*(long *)(&(b)[(o)>>3])>>((o)&0x7))&(m); \
  155.      (o) += (n); \
  156.    }
  157. #else
  158. #  define input(b,o,c,n,m){ \
  159.      REG1 char_type *p = &(b)[(o)>>3]; \
  160.      (c) = ((((long)(p[0]))|((long)(p[1])<<8)| \
  161.      ((long)(p[2])<<16))>>((o)&0x7))&(m); \
  162.      (o) += (n); \
  163.    }
  164. #endif
  165.  
  166. #ifndef MAXSEG_64K
  167.    /* DECLARE(ush, tab_prefix, (1<<BITS)); -- prefix code */
  168. #  define tab_prefixof(i) tab_prefix[i]
  169. #  define clear_tab_prefixof()    memzero(tab_prefix, 256);
  170. #else
  171.    /* DECLARE(ush, tab_prefix0, (1<<(BITS-1)); -- prefix for even codes */
  172.    /* DECLARE(ush, tab_prefix1, (1<<(BITS-1)); -- prefix for odd  codes */
  173.    ush *tab_prefix[2];
  174. #  define tab_prefixof(i) tab_prefix[(i)&1][(i)>>1]
  175. #  define clear_tab_prefixof()    \
  176.       memzero(tab_prefix0, 128), \
  177.       memzero(tab_prefix1, 128);
  178. #endif
  179. #define de_stack        ((char_type *)(&d_buf[DIST_BUFSIZE-1]))
  180. #define tab_suffixof(i) tab_suffix[i]
  181.  
  182. int block_mode = BLOCK_MODE; /* block compress mode -C compatible with 2.0 */
  183.  
  184. /* ============================================================================
  185.  * Decompress in to out.  This routine adapts to the codes in the
  186.  * file building the "string" table on-the-fly; requiring no table to
  187.  * be stored in the compressed file.
  188.  * IN assertions: the buffer inbuf contains already the beginning of
  189.  *   the compressed data, from offsets iptr to insize-1 included.
  190.  *   The magic header has already been checked and skipped.
  191.  *   bytes_in and bytes_out have been initialized.
  192.  */
  193. int unlzw(in, out) 
  194.     int in, out;    /* input and output file descriptors */
  195. {
  196.     REG2   char_type  *stackp;
  197.     REG3   code_int   code;
  198.     REG4   int        finchar;
  199.     REG5   code_int   oldcode;
  200.     REG6   code_int   incode;
  201.     REG7   long       inbits;
  202.     REG8   long       posbits;
  203.     REG9   int        outpos;
  204. /*  REG10  int        insize; (global) */
  205.     REG11  unsigned   bitmask;
  206.     REG12  code_int   free_ent;
  207.     REG13  code_int   maxcode;
  208.     REG14  code_int   maxmaxcode;
  209.     REG15  int        n_bits;
  210.     REG16  int        rsize;
  211.     
  212. #ifdef MAXSEG_64K
  213.     tab_prefix[0] = tab_prefix0;
  214.     tab_prefix[1] = tab_prefix1;
  215. #endif
  216.     maxbits = get_byte();
  217.     block_mode = maxbits & BLOCK_MODE;
  218.     if ((maxbits & LZW_RESERVED) != 0) {
  219.     WARN((stderr, "\n%s: %s: warning, unknown flags 0x%x\n",
  220.           progname, ifname, maxbits & LZW_RESERVED));
  221.     }
  222.     maxbits &= BIT_MASK;
  223.     maxmaxcode = MAXCODE(maxbits);
  224.     
  225.     if (maxbits > BITS) {
  226.     fprintf(stderr,
  227.         "\n%s: %s: compressed with %d bits, can only handle %d bits\n",
  228.         progname, ifname, maxbits, BITS);
  229.     exit_code = ERROR;
  230.     return ERROR;
  231.     }
  232.     rsize = insize;
  233.     maxcode = MAXCODE(n_bits = INIT_BITS)-1;
  234.     bitmask = (1<<n_bits)-1;
  235.     oldcode = -1;
  236.     finchar = 0;
  237.     outpos = 0;
  238.     posbits = inptr<<3;
  239.  
  240.     free_ent = ((block_mode) ? FIRST : 256);
  241.     
  242.     clear_tab_prefixof(); /* Initialize the first 256 entries in the table. */
  243.     
  244.     for (code = 255 ; code >= 0 ; --code) {
  245.     tab_suffixof(code) = (char_type)code;
  246.     }
  247.     do {
  248.     REG1 int i;
  249.     int  e;
  250.     int  o;
  251.     
  252.     resetbuf:
  253.     e = insize-(o = (posbits>>3));
  254.     
  255.     for (i = 0 ; i < e ; ++i) {
  256.         inbuf[i] = inbuf[i+o];
  257.     }
  258.     insize = e;
  259.     posbits = 0;
  260.     
  261.     if (insize < INBUF_EXTRA) {
  262.         if ((rsize = read(in, (char*)inbuf+insize, INBUFSIZ)) == EOF) {
  263.         read_error();
  264.         }
  265.         insize += rsize;
  266.         bytes_in += (ulg)rsize;
  267.     }
  268.     inbits = ((rsize != 0) ? ((long)insize - insize%n_bits)<<3 : 
  269.           ((long)insize<<3)-(n_bits-1));
  270.     
  271.     while (inbits > posbits) {
  272.         if (free_ent > maxcode) {
  273.         posbits = ((posbits-1) +
  274.                ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
  275.         ++n_bits;
  276.         if (n_bits == maxbits) {
  277.             maxcode = maxmaxcode;
  278.         } else {
  279.             maxcode = MAXCODE(n_bits)-1;
  280.         }
  281.         bitmask = (1<<n_bits)-1;
  282.         goto resetbuf;
  283.         }
  284.         input(inbuf,posbits,code,n_bits,bitmask);
  285.         
  286.         if (oldcode == -1) {
  287.         outbuf[outpos++] = (char_type)(finchar = (int)(oldcode=code));
  288.         continue;
  289.         }
  290.         if (code == CLEAR && block_mode) {
  291.         clear_tab_prefixof();
  292.         free_ent = FIRST - 1;
  293.         posbits = ((posbits-1) +
  294.                ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
  295.         maxcode = MAXCODE(n_bits = INIT_BITS)-1;
  296.         bitmask = (1<<n_bits)-1;
  297.         goto resetbuf;
  298.         }
  299.         incode = code;
  300.         stackp = de_stack;
  301.         
  302.         if (code >= free_ent) { /* Special case for KwKwK string. */
  303.         if (code > free_ent) {
  304. #ifdef DEBUG            
  305.             char_type *p;
  306.  
  307.             posbits -= n_bits;
  308.             p = &inbuf[posbits>>3];
  309.             fprintf(stderr,
  310.                 "code:%ld free_ent:%ld n_bits:%d insize:%u\n",
  311.                 code, free_ent, n_bits, insize);
  312.             fprintf(stderr,
  313.                 "posbits:%ld inbuf:%02X %02X %02X %02X %02X\n",
  314.                 posbits, p[-1],p[0],p[1],p[2],p[3]);
  315. #endif
  316.             if (!test && outpos > 0) {
  317.             write_buf(out, (char*)outbuf, outpos);
  318.             bytes_out += (ulg)outpos;
  319.             }
  320.             error("corrupt input. Use zcat to recover some data.");
  321.         }
  322.         *--stackp = (char_type)finchar;
  323.         code = oldcode;
  324.         }
  325.  
  326.         while ((cmp_code_int)code >= (cmp_code_int)256) {
  327.         /* Generate output characters in reverse order */
  328.         *--stackp = tab_suffixof(code);
  329.         code = tab_prefixof(code);
  330.         }
  331.         *--stackp =    (char_type)(finchar = tab_suffixof(code));
  332.         
  333.         /* And put them out in forward order */
  334.         {
  335.         REG1 int    i;
  336.         
  337.         if (outpos+(i = (de_stack-stackp)) >= OUTBUFSIZ) {
  338.             do {
  339.             if (i > OUTBUFSIZ-outpos) i = OUTBUFSIZ-outpos;
  340.  
  341.             if (i > 0) {
  342.                 memcpy(outbuf+outpos, stackp, i);
  343.                 outpos += i;
  344.             }
  345.             if (outpos >= OUTBUFSIZ) {
  346.                 if (!test) {
  347.                 write_buf(out, (char*)outbuf, outpos);
  348.                 bytes_out += (ulg)outpos;
  349.                 }
  350.                 outpos = 0;
  351.             }
  352.             stackp+= i;
  353.             } while ((i = (de_stack-stackp)) > 0);
  354.         } else {
  355.             memcpy(outbuf+outpos, stackp, i);
  356.             outpos += i;
  357.         }
  358.         }
  359.  
  360.         if ((code = free_ent) < maxmaxcode) { /* Generate the new entry. */
  361.  
  362.         tab_prefixof(code) = (unsigned short)oldcode;
  363.         tab_suffixof(code) = (char_type)finchar;
  364.         free_ent = code+1;
  365.         } 
  366.         oldcode = incode;    /* Remember previous code.    */
  367.     }
  368.     } while (rsize != 0);
  369.     
  370.     if (!test && outpos > 0) {
  371.     write_buf(out, (char*)outbuf, outpos);
  372.     bytes_out += (ulg)outpos;
  373.     }
  374.     return OK;
  375. }
  376.